package com.yupi.project.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.project.mapper.SignInfoMapper;
import com.yupi.project.service.SignInfoService;
import com.yupi.yuapicommon.model.entity.SignInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * @description: 签到服务
 * @author SummerBird
 * @date 2024/2/19 11:15
 * @version 1.0
 */
@Service
@Slf4j
public class SignInfoServiceImpl extends ServiceImpl<SignInfoMapper, SignInfo>
        implements SignInfoService {

    @Override
    public boolean sign(long userId) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年份
        int year = currentDate.getYear();
        // 获取当前月份
        int month = currentDate.getMonthValue();
        // 获取当前日期（即当前月的第几天）
        int dayOfMonth = currentDate.getDayOfMonth();

        System.out.println("当前日期：" + year + "-" + month + "-" + dayOfMonth);
        LambdaQueryWrapper<SignInfo> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SignInfo::getUserId, userId);
        lambdaQueryWrapper.eq(SignInfo::getYear, year);
        lambdaQueryWrapper.eq(SignInfo::getMonth, month);
        SignInfo signInfo = this.getOne(lambdaQueryWrapper);
        if(signInfo == null){
            signInfo = new SignInfo();
            signInfo.setUserId(userId);
            signInfo.setYear(year);
            signInfo.setMonth(month);
            signInfo.setSignOfMonth(getSignOfMonth(0, dayOfMonth));
            signInfo.setIsContinue(1);
            signInfo.setCreateTime(String.valueOf(new DateTime(System.currentTimeMillis())));
            signInfo.setUpdateTime(String.valueOf(new DateTime(System.currentTimeMillis())));
        }else {
            Integer signOfMonth = signInfo.getSignOfMonth();
            signInfo.setSignOfMonth(getSignOfMonth(signOfMonth, dayOfMonth));
            signInfo.setUpdateTime(String.valueOf(new DateTime(System.currentTimeMillis())));
        }
        return this.saveOrUpdate(signInfo);
    }

    @Override
    public Boolean isSign(Long userId) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年份
        int year = currentDate.getYear();
        // 获取当前月份
        int month = currentDate.getMonthValue();
        // 获取当前日期（即当前月的第几天）
        int dayOfMonth = currentDate.getDayOfMonth();
        System.out.println("当前日期：" + year + "-" + month + "-" + dayOfMonth);
        LambdaQueryWrapper<SignInfo> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SignInfo::getUserId, userId);
        lambdaQueryWrapper.eq(SignInfo::getYear, year);
        lambdaQueryWrapper.eq(SignInfo::getMonth, month);
        SignInfo signInfo = this.getOne(lambdaQueryWrapper);
        if (signInfo == null){
            return false;
        }else {
            return (signInfo.getSignOfMonth() & (1 << (dayOfMonth - 1))) != 0;
        }
    }

    private static Integer getSignOfMonth(int signOfMonth, int dayOfMonth){
        signOfMonth |= (1 << (dayOfMonth - 1));
        return signOfMonth;
    }

    public static void main(String[] args) {
        System.out.println(Integer.toBinaryString(Integer.valueOf(2147483647)));
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取当前年份
        int year = currentDate.getYear();

        // 获取当前月份
        int month = currentDate.getMonthValue();

        // 获取当前日期（即当前月的第几天）
        int dayOfMonth = currentDate.getDayOfMonth();

        System.out.println("当前日期：" + year + "-" + month + "-" + dayOfMonth);

        // 计算该日是该月的第几天
        int dayOfYear = currentDate.getDayOfYear();

        System.out.println("该日是该月的第几天：" + dayOfMonth);

        System.out.println(Integer.toBinaryString(getSignOfMonth(0, dayOfMonth)));
    }
}
